package com.intelligentpension.utils

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.wifi.ScanResult
import android.net.wifi.WifiManager
import android.os.Build
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import com.hjq.toast.ToastUtils
import com.intelligentpension.kotlinwifi.MainActivity
import com.intelligentpension.kotlinwifi.SendAndReceivedActivity
import com.thanosfisherman.wifiutils.WifiUtils
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener

/**
 * 版权：点赞 版权所有
 * author :赵航
 * @e-mail: zhaohangcn@gmail.com
 * time：2022/4/9
 * describe ：
 */
class WifiMangerUtil(var context: Context) {
    private var mWifiManager: WifiManager? = null

    init {
        mWifiManager =
            (context.applicationContext.getSystemService(Context.WIFI_SERVICE)) as WifiManager
    }

    /**
     * 获取wifimanager
     */
    fun getWifiManger(): WifiManager? {
        return mWifiManager
    }

    /**
     *  TODO:必须大于21才能检测是否支持wifi
     */
    fun isSupportedWifi(): Boolean {
        var support = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (mWifiManager!!.isWapiSupported) {
                ToastUtils.show("该设备没有wifi功能")
                support = false
            }
        }
        return support
    }

    /**
     * 是否开启wifi，没有的话打开wifi
     */
    fun openWifi(): Boolean {
        var bRet = true
        if (!mWifiManager!!.isWifiEnabled) {
            bRet = mWifiManager!!.setWifiEnabled(true)
        }
        return bRet
    }

    /**
     * 扫描wifi
     */
    fun startScanWifi() {
        mWifiManager!!.startScan()
    }

    /**
     * 获取wifi列表
     */
    fun getWifiList(): List<ScanResult> {
        return mWifiManager!!.scanResults
    }

    /**
     * 点击列表.连接指定的wifi
     */
    fun router(wifiName: String) {
        WifiUtils.withContext(context)
            .connectWith(wifiName, "")
            .setTimeout(15000)
            .onConnectionResult(object : ConnectionSuccessListener {
                override fun success() {
                    Toast.makeText(context, "连接成功!", Toast.LENGTH_SHORT).show()
                    if (is5GBandLimits()){
                        Toast.makeText(context, "不能把HC-25模块配置到5G频段的路由!", Toast.LENGTH_SHORT).show()
                    }
                    if (wifiName.contains("HC-25")){
                        Intent(context,SendAndReceivedActivity::class.java).apply {
                            context.startActivity(this)
                        }
                    }

                }

                override fun failed(errorCode: ConnectionErrorCode) {
                    Toast.makeText(context, "EPIC FAIL!$errorCode", Toast.LENGTH_SHORT).show()
                }
            })
            .start()
    }

    //判断当前是否为5g频段
    fun is5GBandLimits(): Boolean {
        var freg: Int = 0
        var wifiInfo = mWifiManager!!.connectionInfo
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            freg = wifiInfo.frequency
        } else {
            var nowRouter = mWifiManager!!.connectionInfo.ssid.substring(
                1, mWifiManager!!.connectionInfo.ssid.length - 1
            )

            for (scanResult in mWifiManager!!.scanResults) {
                if (nowRouter == scanResult.SSID) {
                    freg = scanResult.frequency
                }
            }
        }

        return freg in 4901..5899
    }

}